const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainId: null,
    positionTrains: [],
    currentIndex: 0,
    movements: [],
    positions: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const trainId = wx.getStorageSync('trainId');
    const globalPositionTrains = app.globalData.positionTrains;
    const positionTrains = [];
    globalPositionTrains.forEach(function(item) {
      if (item.name.indexOf('减脂') < 0) {
        const positionTrain = {};
        positionTrain.name = item.name.substring(0, 2);
        positionTrain.movements = [];
        item.movements.forEach(function(movement) {
          const m = {
            name: movement,
            selected: false
          };
          positionTrain.movements.push(m);
        });
        positionTrains.push(positionTrain);
      }
    });
    this.setData({
      trainId,
      positionTrains,
      movements: positionTrains[0].movements
    });
    this.fetchEms();
  },
  fetchEms: function() {
    const traineePhone = wx.getStorageSync('phone');
    const url = app.globalData.url;
    const positions = this.data.positions;
    const that = this;
    wx.request({
      url: url + '/train/emsAssessment/' + traineePhone,
      success: function(response) {
        if (response.data.success) {
          const emsObj = response.data.content;
          positions.forEach(function(position) {
            switch (position.columnName) {
              case 'ertou':
                position.ertou1 = emsObj.ertou1;
                position.ertou2 = emsObj.ertou2;
                break;
              case 'santou':
                position.santou1 = emsObj.santou1;
                position.santou2 = emsObj.santou2;
                break;
              case 'xiongbu':
                position.xiongbu1 = emsObj.xiongbu1;
                position.xiongbu2 = emsObj.xiongbu2;
                break;
              case 'fubu':
                position.fubu1 = emsObj.fubu1;
                position.fubu2 = emsObj.fubu2;
                break;
              case 'shangbei':
                position.shangbei1 = emsObj.shangbei1;
                position.shangbei2 = emsObj.shangbei2;
                break;
              case 'beikuo':
                position.beikuo1 = emsObj.beikuo1;
                position.beikuo2 = emsObj.beikuo2;
                break;
              case 'xiabei':
                position.xiabei1 = emsObj.xiabei1;
                position.xiabei2 = emsObj.xiabei2;
                break;
              case 'tunbu':
                position.tunbu1 = emsObj.tunbu1;
                position.tunbu2 = emsObj.tunbu2;
                break;
              case 'qiantui':
                position.qiantui1 = emsObj.qiantui1;
                position.qiantui2 = emsObj.qiantui2;
                break;
              case 'houtui':
                position.houtui1 = emsObj.houtui1;
                position.houtui2 = emsObj.houtui2;
                break;
            }
          });
          that.setData({
            positions
          });
        }
      }
    });
  },
  getEmsValue: function (position) {
    const positions = this.data.positions;
    const size = positions.length;
    for (let i = 0; i < size; i++) {
      const selectPosition = positions[i];
      if (position === selectPosition.position) {
        return selectPosition[selectPosition.columnName + '1'];
      }
    }
  },
  change: function(e) {
    const index = this.data.currentIndex;
    const movementName = e.currentTarget.dataset.movement;
    const positionTrains = this.data.positionTrains;
    const selectPosition = positionTrains[index];
    selectPosition.movements.forEach(function(movement) {
      if (movement.name == movementName) {
        movement.selected = !movement.selected;
      }
    });
    this.setData({
      positionTrains,
      movements: positionTrains[index].movements
    });
  },
  changePosition: function(e) {
    const index = e.currentTarget.dataset.index;
    const positionTrains = this.data.positionTrains;
    this.setData({
      currentIndex: index,
      movements: positionTrains[index].movements
    });
  },
  save: function() {
    const positionTrains = this.data.positionTrains;
    const selectPositionTrains = [];
    const that = this;
    positionTrains.forEach(function(train) {
      const selectMovements = [];
      train.movements.forEach(function(movement) {
        if (movement.selected == true) {
          selectMovements.push(movement.name);
        }
      });
      if (selectMovements.length > 0) {
        selectPositionTrains.push({
          name: train.name + '训练',
          movements: selectMovements
        });
      }
    });
    if (selectPositionTrains.length > 0) {
      wx.showLoading({
        title: '保存中...'
      });
      const courseTrains = [];
      const positionTrains = app.globalData.positionTrains;
      selectPositionTrains.forEach(function(train) {
        positionTrains.forEach(function(positionTrain) {
          if (train.name === positionTrain.name) {
            const courseTrain = {
              name: train.name
            };
            const items = [];
            if (positionTrain.normal) {
              positionTrain.normal.forEach(function (item) {
                const emsValue = that.getEmsValue(item) || 0;
                items.push({
                  position: item,
                  strongValue: emsValue
                });
              });
            }
            courseTrain.trainPrograms = [];
            train.movements.forEach(function(m){
              const trainProgram = { items, name: m, strongOption:'NORMAL'};
              courseTrain.trainPrograms.push(trainProgram);
            });
            courseTrains.push(courseTrain);
          }
        });
      });
      const url = app.globalData.url;
      const trainId = this.data.trainId;
      const trainPrograms = this.data.trainPrograms;
      wx.request({
        url: url + '/train/addPrograms',
        data: {
          dailyTrainId: trainId,
          courseTrains
        },
        method: 'POST',
        success: function (response) {
          if (response.data.success) {
            wx.navigateBack({
              delta: 2
            });
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: response.data.message || '保存失败',
              icon: 'none'
            })
          }
        }
      })
    } else {
      wx.navigateBack({
        delta: 2
      });
    }
  }
})